<?php

	require_once(dirname(__FILE__)."/config/include.php");
	try{
		ob_start();
		$val = getPostValue() ;
		$call_raw_data = $GLOBALS['HTTP_RAW_POST_DATA'] ;
		if (strlen($call_raw_data) > 512)	$call_raw_data = substr($call_raw_data, 0 , 256) ;
		
		if ($GLOBALS['DEBUG_CALL_LOG'] == true){
			$time = date("Ymd H:i:s") ;
			file_put_contents(dirname(__FILE__)."/debug.txt","\r\n $time : $call_raw_data",FILE_APPEND);
		}
		
		$commandFunc 	= $val[0] ;
		$commandParam 	= $val[1] ;
		$session	 	= $val[2] ;
		
		if (function_exists($commandFunc))
	    {
	    	sql_connect();
	    	$ret = $commandFunc($commandParam , $session);
			if(array_key_exists('cmd', $ret) == false)		$ret['cmd'] = $commandFunc ;
			if(array_key_exists('status', $ret) == false)	$ret['status'] = $GLOBALS["ERR"]["OK"] ;
	    	ob_end_clean();
	    	$show = json_encode($ret) ;
	    	echo $show ;
	    	ob_end_flush();
	    	
	    	if ($GLOBALS['DEBUG_CALL_LOG'] == true){
				$time = date("Ymd H:i:s") ;
				$logshow = json_encode_utf8($ret) ;
				file_put_contents(dirname(__FILE__)."/debug.txt","\r\n $time : $logshow",FILE_APPEND);
			}
	    }else if ($GLOBALS['HTTP_RAW_POST_DATA'] != ""){
			$j_ok = "" ;			
	    	if ($jsonObj == null){
				$j_ok = " , Json format conversion failure " ;
			}
	    	$ret = array() ;
			$ret['cmd'] = $commandFunc ;
			$ret['status'] = $GLOBALS["ERR"]["ERR_CALL_NO_CMD"] ;
			$ret['message'] = $GLOBALS["MES"]["ERR_CALL_NO_CMD"]."[$commandFunc] , $j_ok ." ;
			$ret['call'] = $call_raw_data ;
			
	    	ob_end_clean();
			$show = json_encode($ret) ; // json_encode_utf8($ret) ;
	    	echo $show ;
			ob_end_flush();
			
			if ($GLOBALS['DEBUG_ERR_LOG'] == true){
				$time = date("Ymd H:i:s") ;
				$logshow = json_encode_utf8($ret) ;
				file_put_contents(dirname(__FILE__)."/error.txt","\r\n $time : $logshow",FILE_APPEND);
			}
	    }
	}catch(Exception $e){
		$ret = array() ;
		$ret['cmd'] = "error" ;
		$ret['status'] = $GLOBALS["ERR"]["ERR_CALL_TRY"] ;
		$ret['message'] = $GLOBALS["MES"]["ERR_CALL_TRY"]." , ".$e->getMessage() ;
		$ret['call'] = $call_raw_data ;
		
		ob_end_clean();
		$show = json_encode($ret) ; // json_encode_utf8($ret) ;
	    echo $show ;
		ob_end_flush();
		
		if ($GLOBALS['DEBUG_ERR_LOG'] == true){
			$time = date("Ymd H:i:s") ;
			$logshow = json_encode_utf8($ret) ;
			file_put_contents(dirname(__FILE__)."/error.txt","\r\n $time : $logshow  -|||-  ".exceptionToString($e),FILE_APPEND);
		}
	}
?>